package com.wang.springbootinit.minio;

import com.wang.springbootinit.common.BaseResponse;
import com.wang.springbootinit.common.ErrorCode;
import com.wang.springbootinit.common.ResultUtils;
import com.wang.springbootinit.config.MinioConfig;
import com.wang.springbootinit.constant.FileEnum;
import io.minio.messages.Bucket;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Slf4j
@RestController
@RequestMapping(value = "/demo/demo/file")
public class FileDemoController {
 
    @Autowired
    private MinioUtil minioUtil;
    @Autowired
    private MinioConfig prop;
 
    @ApiOperation(value = "查看存储bucket是否存在")
    @GetMapping("/bucketExists")
    public BaseResponse<Boolean> bucketExists(@RequestParam("bucketName") String bucketName) {
        return ResultUtils.success(minioUtil.bucketExists(bucketName));
    }
 
    @ApiOperation(value = "创建存储bucket")
    @GetMapping("/makeBucket")
    public BaseResponse<Boolean> makeBucket(@RequestParam("bucketName")String bucketName) {
        return ResultUtils.success(minioUtil.makeBucket(bucketName));
    }
 
    @ApiOperation(value = "删除存储bucket")
    @GetMapping("/removeBucket")
    public BaseResponse<Boolean> removeBucket(String bucketName) {
        return ResultUtils.success(minioUtil.removeBucket(bucketName));
    }
 
    @ApiOperation(value = "获取全部bucket")
    @GetMapping("/getAllBuckets")
    public BaseResponse<List<Bucket>> getAllBuckets() {
        List<Bucket> allBuckets = minioUtil.getAllBuckets();
        return ResultUtils.success(allBuckets);

    }
 
    @ApiOperation(value = "文件上传返回url")
    @PostMapping("/upload")
    public BaseResponse<List<String>> upload(@RequestParam("file") MultipartFile file) {
        List<String> fileNameList = minioUtil.upload(file, FileEnum.SOURCE_FILE.getBucketName());
//        if (null != objectName) {
//            String result = prop.getEndpoint() + "/" + prop.getBucketName() +
//                    "/" + objectName;
//
//        }
        return ResultUtils.success(fileNameList);
//        return ResultUtils.error(ErrorCode.SYSTEM_ERROR);
    }
 
    @ApiOperation(value = "图片/视频预览")
    @GetMapping("/preview")
    public BaseResponse<String> preview(@RequestParam("fileName") String fileName) {
        return ResultUtils.success(minioUtil.preview(fileName));
    }
 
    @ApiOperation(value = "文件下载")
    @GetMapping("/download")
    public BaseResponse<Boolean> download(@RequestParam("fileName") String fileName, HttpServletResponse res)
    {
//        minioUtil.download(fileName,res);
        return ResultUtils.success(Boolean.TRUE);
    }
 
    @ApiOperation(value = "删除文件", notes = "根据url地址删除文件")
    @PostMapping("/delete")
    public BaseResponse<String> remove(@RequestBody FileRequestParam param) {
        String url = param.getUrl();
        String objName = url.substring(url.lastIndexOf(prop.getBucketName()+"/") + 
        prop.getBucketName().length()+1);
        boolean remove = minioUtil.remove(objName);
        System.out.println(remove);
        return ResultUtils.success(objName);
    }
 
}
